﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace xna_game_engine.source
{
    public class State_Gameplay : State_Base
    {
        // Members
        CGameScreen mScreen;

        // Functions
        public State_Gameplay(StateMachine.States Id) : base(Id)
        {
            // Gameplay stuff here
            GameGlobals.mPlayers.Add(new FireWizard(PlayerIndex.One));
            GameGlobals.mPlayers.Add(new FireWizard(PlayerIndex.Two));

            mName = "Gameplay";
            mScreen = new CGameScreen();
            mScreen.AddElement(new CScreenElementImage("screens//test_bed_level", Vector2.Zero));
            mScreen.AddElement(new CScreenImageUnitFrame(Vector2.Zero, GameGlobals.mPlayers[0]));
            mScreen.AddElement(new CScreenImageUnitFrame(new Vector2(649.0f, 0.0f), GameGlobals.mPlayers[1]));

            // Add our event handlers...
            mEventHandlers[EventManager.Events.EVT_START_HIT] = new EventHandler(OnPauseHit);
            //mEventHandlers[EventManager.Events.EVT_ENTER_HIT] = new EventHandler(OnEnterHit);
            mEventHandlers[EventManager.Events.EVT_R_TRIGGER_HIT] = new EventHandler(OnRightTrigger);
            mEventHandlers[EventManager.Events.EVT_B_HIT] = new EventHandler(OnBButtonHit);
            mEventHandlers[EventManager.Events.EVT_Y_HIT] = new EventHandler(OnYButtonHit);
            mEventHandlers[EventManager.Events.EVT_SELECT_HIT] = new EventHandler(OnSelectHit);
            mEventHandlers[EventManager.Events.EVT_R_BUMPER_HIT] = new EventHandler(SpawnEnemy);
            mEventHandlers[EventManager.Events.EVT_L_BUMPER_HIT] = new EventHandler(TestTalentPoint);
        }

        private void TestTalentPoint()
        {
            //GameGlobals.mPlayers[0].AddTalentPoint(0);
        }

        private void OnPauseHit()
        {
            Console.WriteLine("Gameplay: Transitioning to Pause State...");
            GameSystems.Audio.PauseSoundCue("requiem");
            GameSystems.Audio.PlaySoundCue("level_up");
            GameGlobals.StateMachine.Transition(StateMachine.States.STATE_PAUSE);
        }

        private void OnEnterHit()
        {
            Console.WriteLine("Gameplay: Transitioning to Main Menu...");
            GameSystems.Audio.StopSoundCue("requiem");
            GameGlobals.StateMachine.Transition(StateMachine.States.STATE_MAIN_MENU); 
        }

        private void OnSelectHit()
        {
            Console.WriteLine("Gameplay: Transitioning to Char Info...");
            GameSystems.Audio.PauseSoundCue("requiem");
            GameGlobals.StateMachine.Transition(StateMachine.States.STATE_CHAR_INFO); 
        }

        private void OnRightTrigger()
        {
            //GameGlobals.mPlayers[0].LevelUp();
            GameGlobals.mPlayers[0].OnSpellUse(BaseWizard.FaceButtons.R_TRIGGER);
        }

        private void OnBButtonHit()
        {
            //GameGlobals.mPlayers[0].LevelUp();
            GameGlobals.mPlayers[0].OnSpellUse(BaseWizard.FaceButtons.B_BUTTON);
        }

        private void OnYButtonHit()
        {
            //GameGlobals.mPlayers[0].LevelUp();
            GameGlobals.mPlayers[1].OnSpellUse(BaseWizard.FaceButtons.Y_BUTTON);
        }
        

        private void SpawnEnemy()
        {
            Console.WriteLine("Spawning Enemy...");
            GameGlobals.EnemyManager.SpawnEnemy(new Vector2(GameGlobals.RNG.Next(100, 900), GameGlobals.RNG.Next(100, 700)));
        }

        // Parental Overrides (forced)
        public override void  Init()
        {
            Console.WriteLine("Gameplay Init()...");
            mScreen.Init();
            //throw new System.NotImplementedException();
        }

        public override void Enter()
        {
            Console.WriteLine("Gameplay Enter()...");
            //GameSystems.Audio.PlaySoundCue("requiem");
            //throw new NotImplementedException();
        }

        public override void Update(GameTime gameTime)
        {
            //Console.WriteLine("Gameplay Update()...");
            for (int i = 0; i < GameGlobals.mPlayers.Count; i++)
            {
                GameGlobals.mPlayers[i].Update(gameTime);
                GameGlobals.SpellManager.CheckCollisions(GameGlobals.mPlayers[i]);
                if (GameGlobals.mPlayers[i].CurHealth == 0)
                {
                    // TODO: Kill player
                    GameGlobals.mPlayers.RemoveAt(i);
                }
            }
            //GameGlobals.MessageBox.Update(gameTime);
            GameGlobals.CombatText.Update(gameTime);
            GameSystems.Graphics.Update(gameTime);
            GameGlobals.SpellManager.Update(gameTime);
            GameGlobals.LootManager.Update(gameTime);
            mScreen.Update(gameTime);
           // GameGlobals.StateMachine.Update(gameTime);
        }

        public override void Exit()
        {
            Console.WriteLine("Gameplay Exit()...");
            //throw new NotImplementedException();
        }

        public override void Draw()
        {
            mScreen.Draw();
            foreach (BaseWizard player in GameGlobals.mPlayers)
            {
                player.Draw();
            }
            GameGlobals.SpellManager.Draw();
            GameGlobals.CombatText.Draw();
            GameGlobals.LootManager.Draw();
            //Console.WriteLine("Drawing Gameplay screen!");
            //throw new NotImplementedException();
        }
    }
}
